#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;
using Utilities.Testing;

namespace Utilities.Test.Testing
{
  public class ClassTesterFixture : TestFixtureBase
  {
    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: subject")]
    public void TestConstructorError()
    {
      new ClassTester(null);
    }

    [Test]
    public void TestSelf()
    {
      ClassTester tester = new ClassTester(new ClassTester(new object()));
      tester.TestProperties();
    }

    [Test]
    public void TestPass()
    {
      ClassTester tester = new ClassTester(new MockGood());
      tester.TestProperties();
    }

    [Test]
    [ExpectedException(typeof(ClassTesterException), ExpectedMessage = @"The get value of the 'SomeString' property did not equal the set value (in: 'Pie 2: This time it's personal. ', out: 'Pie 2: This time it's personal.')")]
    public void TestBadProperty()
    {
      IValueCreator mockValueCreator = Repository.CreateMock<IValueCreator>();
      using (Repository.Record())
      {
        Expect.Call(mockValueCreator.CanCreateValue(typeof(string))).Return(true);
        Expect.Call(mockValueCreator.CreateValue(typeof(string))).Return("Pie");
        Expect.Call(mockValueCreator.CreateValue(typeof(string))).Return("Pie 2: This time it's personal.");
      }
      using (Repository.Playback())
      {
        ClassTester tester = new ClassTester(new MockBadProperty(), mockValueCreator);
        tester.TestProperties();
      }
    }

    [Test]
    public void TestBadPropertySkipped()
    {
      ClassTester tester = new ClassTester(new MockBadProperty());
      tester.IgnoredProperties.Add("SomeString");
      tester.TestProperties();
    }

    [Test]
    [ExpectedException(typeof(ClassTesterException), ExpectedMessage = "The property 'SomeInt' did not throw a PropertyChangedEvent")]
    public void TestNoEvent()
    {
      ClassTester tester = new ClassTester(new MockNoEvent());
      tester.TestProperties();
    }

    [Test]
    public void TestNoEventSkipped()
    {
      ClassTester tester = new ClassTester(new MockNoEvent());
      tester.IgnoredProperties.Add("SomeInt");
      tester.TestProperties();
    }

    [Test]
    public void TestGenericClass()
    {
      ClassTester tester = new ClassTester(new DummyGeneric<int, int?, List<string>, string>());
      tester.TestProperties();
    }

    [Test]
    [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Could not generate two different values for the type 'Utilities.Test.Testing.MockWithSameEquals'. Consider ignoring the 'DummyWithSameEquals' property or increasing the MaxLoopsPerProperty value above 1000.")]
    public void TestCannotGenerateTwoDifferentValues()
    {
      ClassTester tester = new ClassTester(new DummyUsesSameEquals());
      tester.TestProperties();
    }

    [Test]
    public void TestDummyGood()
    {
      ClassTester.TestConstructors(typeof(MockGood), true);
    }

    [Test]
    [ExpectedException(typeof(ClassTesterException), ExpectedMessage = @"The value of the 'MyString' property did not equal the value set with the 'myString' constructor parameter (in: 'Pie ', out: 'Pie')")]
    public void TestDummyConstructorNotMapped()
    {
      IValueCreator mockValueCreator;
      using (Repository.Record())
      {
        mockValueCreator = Repository.CreateMock<IValueCreator>();
        Expect.Call(mockValueCreator.CreateValue(typeof(string))).Return("Pie");
      }
      using (Repository.Playback())
      {
        ClassTester.TestConstructors(typeof(MockConstructorNotMapped), true, mockValueCreator);
      }
    }

    [Test]
    public void TestDummyConstructorNotMappedSkipped()
    {
      ClassTester.TestConstructors(typeof(MockConstructorNotMapped), false);
    }

    [Test]
    public void TestGeneric()
    {
      ClassTester.TestConstructors(typeof(DummyGeneric<string, int, int?, MyEnum>), false);
    }

    public interface IMockClassTesterCollection
    {
      int[] IntArray { get; set; }
      List<string> StringList { get; set; }
      IList<string> StringListInterface { get; set; }
    }

    [Test]
    public void TestCollections()
    {
      IMockClassTesterCollection mockCollectionClass = Repository.Stub<IMockClassTesterCollection>();
      Repository.ReplayAll();

      using (Repository.Playback())
      {
        ClassTester tester = new ClassTester(mockCollectionClass);
        tester.TestProperties();
      }
    }
  }
}